/*****************************获取单个用户信息*********************/    
    var caseId=GetQueryString('id')
    var baseId=0
    var beforStatus=0
    var nowStatus
    var baseData
    var moreData
    var thisurl=document.location.href
    localStorage.setItem('casebackurl',thisurl)
    var getOneCaseData={
            url:'minor_message/showMinorApi',
            fromwho:'ld',
            sendData:{
                id:caseId
            }
        }
    getAjax(getOneCaseData,
                function(data){
                    if(data.result==1){
                        baseData=data.data.base_msg
                        moreData=data.data.schedule_list
                        baseId=baseData.id
                        console.log(baseData)
                        console.log(moreData)
                        setTimeout(function(){
                            setOneCaseData(baseData,moreData)
                        },100);
                    }else{
                        mui.toast(data.msg);
                    }
                },
                function(data){
                    mui.toast('服务器出错！');
                }
            )
    function setOneCaseData(baseData,moreData){
        //基本信息
        var basehtml=`
                <p class="caseBox"><span>案件编号：</span><label for="">${baseData.case_num}</label></p>
                <p class="caseBox"><span>嫌疑人编号：</span><label for="">${baseData.num}</label></p>
                <p class="caseBox"><span>案件发生时间：</span><label for="">${baseData.case_time}</label></p>
                <p class="caseBox"><span>案件创立时间：</span><label for="">${baseData.create_time}</label></p>
            `
        $('.caseInfo.casebase').prepend(basehtml)
        //案件描素
        var deschtml=`
                <p class="caseBox">${baseData.case_describe}</p>
            `
        $('.caseInfo.casedesc').prepend(deschtml)
        //状态
        var st=0
        switch (baseData.case_status){
            case '审查批捕':
                st=1;
                break;
            case '提起公诉':
                st=2;
                break;
            case '附条件不起诉':
                st=3;
                break;
            default:
                break;
        }
        beforStatus=st
        $('select.caseZt').val(beforStatus)
        nowStatus=baseData.case_status
        //更多
        $.each(moreData,function(k,v){
            if(v.list&&v.list.length>0){
                var html=''
                $.each(v.list,function(k2,v2){
                    if(nowStatus!='附条件不起诉'){
                        $('#protect-div button').remove()
                    }
                    if(nowStatus==v2.case_status){
                        /*获取被指派人*/       
                        var unames=[]
                        var tousers=''
                        if(v2.users&&v2.users.length>0){
                            $.each(v2.users,function(k3,v3){
                                var val=v3.answerer_name+'('+v3.tel+')'
                                unames.push(val)
                            })
                            tousers="指派给了"+unames.join('、')
                        }
                        /*获取被指派人end*/
                        //心理咨询
                        if(v.type=='psy'){
                            html+='<span style="display:block;margin:10px 0">'+v2.reply_name+'于'+v2.creat_time+''+tousers+'。</span>'
                            if(v2.status!='未办理'){
                                html+='<span data-id="'+ v2.apply_id +'"class="apply-span-btn '+v.type+'-showmore" style>查看回复</span>'
                            }else{
                                html+='<span data-id="'+ v2.apply_id +'"class="apply-span-btn '+v.type+'-del">撤回</span>'
                            }
                            html+='</span>'
                        }
                        //观护帮教
                        if(v.type=='protect'){
                            html+='<span style="display:block;margin:10px 0">'+v2.nick_name+'于'+v2.create_time+'发起'+v2.type+'申请，指派给了'+v2.answerer_name+'('+v2.tel+')'+'。当前状态：'+v2.status+'</span>'
                            if(v2.status=='正在观护中'){
                                $('.btn-showprotectreply').attr('data-id',v2.apply_id)
                                html+='<span data-id="'+ v2.apply_id +'"class="apply-span-btn '+v.type+'-change" data-type='+ v2.type+'>修改指派人</span>'
                            }else{
                                $('.btn-showprotectreply').attr('data-id',v2.apply_id)
                            }
                            html+='</span>'
                        }
                        //合适成年人
                        if(v.type=='adult'){
                            if(!v2.reply_name){
                                html+='<span style="display:block;margin:10px 0;border-bottom:1px solid #ccc;padding-bottom: 5px;">'+v2.nick_name+'于'+v2.create_time+'发起申请。</span>'
                            }else{
                                html+='<span style="display:block;margin:10px 0;border-bottom:1px solid #ccc;padding-bottom: 5px;">'+v2.nick_name+'于'+v2.create_time+'发起申请，'+v2.reply_name+'于'+v2.reply_time+'接受委托并'+tousers+'。</span>'
                            }
                        }
                        //法律和社会
                        if(v.type=='law'||v.type=='survey'){
                            if(!v2.reply_name){
                                html+='<span style="display:block;margin:10px 0">'+v2.nick_name+'于'+v2.create_time+'发起申请。</span>'
                            }else{
                                html+='<span style="display:block;margin:10px 0">'+v2.nick_name+'于'+v2.create_time+'发起申请，'+v2.reply_name+'于'+v2.reply_time+'接受委托并'+tousers+'。</span>'
                            }
                            if(v2.status!='未办理'){
                                html+='<span data-id="'+ v2.apply_id +'"class="apply-span-btn '+v.type+'-showmore">查看回复</span>'
                            }else{
                                
                            }
                            html+='</span>'
                        }
                    }
                })
                if(html){
                  $("p.tswz." + v.type).html(html)  
                }
            }
        });
    }
/*****************************获取单个用户信息end*********************/
/*****************************发起待办事项*********************/  
    $('.apply-btn').on('click',function(){
        var applyType=$(this).attr('data-type')
        var caseStatus=$('select.caseZt').val();
        //心里咨询
        if(applyType=='psy'){
            mui.openWindow({
                url:'caseToPsy.html?baseid='+baseId,
                id:'caseToPsy.html?baseid='+baseId,
            });
            return false;
        }
        //观护帮教
        if(applyType=='protect'){
            mui.openWindow({
                url:'caseToPro.html?baseid='+baseId,
                id:'caseToPro.html?baseid='+baseId,
            });
            return false;
        }
        //申请合适成年人3个
        if(applyType=='adult'||applyType=='survey'||applyType=='law'){
            mui.openWindow({
                url:'caseToOther.html?baseid='+baseId+'&atype='+applyType +'&cstatus='+caseStatus ,
                id:'caseToOther.html',
            });
            return false;
        }
    });
/*****************************发起待办事项end*********************/   
/*****************************修改状态*********************/
    $('select.caseZt').on('change',function(){
        var obj=$(this)
        var upCaseAddData={
            url:'minor_message/addEditMinorMsg',
            fromwho:'ld',
            sendData:{
                id:caseId,
                case_status:obj.val()
            }
        }
        upAjax(upCaseAddData,
            function(data){
                if(data.result==1){
                    mui.toast('状态修改成功')
                    setTimeout(function(){
                        location.reload();
                    },100)
                }else{
                    mui.toast(data.msg)
                }
            },
            function(data){
                mui.toast('服务器出错')
            }
        )
    })
/*****************************修改状态end*********************/ 
/*****************************点击查看申请回复*********************/  
    $(document).on('click','.apply-span-btn',function(){
        var aid=$(this).attr('data-id')
        //法律和社会
        if($(this).hasClass('survey-showmore')||$(this).hasClass('law-showmore')){
            mui.openWindow({
                url:"caseShowReply.html?id="+aid,
                id:'caseShowReply.html'
            });
            return false;
        }
        //心理
        if($(this).hasClass('psy-showmore')){
            mui.openWindow({
                url:"caseShowPsyReply.html?id="+aid,
                id:'caseShowPsyReply.html'
            });
            return false;
        }
    })
    //观护帮教
    $(document).on('click','.btn-showprotectreply',function(){
        var aid=$(this).attr('data-id')
        mui.openWindow({
            url:"caseShowProReply.html?baseid="+baseId+'&aid='+aid,
            id:'caseShowProReply.html'
        });
        return false;
    });
/*****************************点击查看申请回复end*********************/    
/*****************************撤回心理咨询*********************/  
    $(document).on('click','.psy-del',function(){
        var obj=$(this)
        var aid=$(this).attr('data-id')
        var delApplyReplay={
            url:"Psychologist_Manage/recallAssign",
            fromwho:'ld',
            sendData:{
                id:aid
            }
        }
        upAjax(delApplyReplay,
            function(data){
                console.log(data)
                if(data.result==1){
                     mui.toast('撤回成功')
                     setTimeout(function(){
                       obj.parents('span').remove()
                    },500)
                }else{
                    mui.toast('撤回失败')
                }
            },
            function(data){
                mui.toast('服务器错误')
            }
        )
    })
    
/*****************************撤回心理咨询end*********************/   
/*****************************修改观护帮教*********************/  
    $(document).on('click','.protect-change',function(){
        var aid=$(this).attr('data-id')
        var type=$(this).attr('data-type')
        mui.openWindow({
            url:"caseChangePro.html?baseid="+baseId+'&aid='+aid+"&type="+type,
            id:"caseChangePro.html"
        });
    })
/*****************************修改观护帮教end*********************/